package org.example;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ClassInfo {

    private static Pattern commentsMatcher = Pattern.compile("/\\*\\*\\s+?\\*\\s+?((?:\\s|.)+?)\\s+?\\*(\\s|.)*?\\*/");
    private static Pattern methodsPattern = Pattern.compile("\\s+?/\\*{2}(.|\\s)+?\\)\\s*?\\{");
    private static Pattern methodPattern = Pattern.compile("public\\s+?((?:static\\s+?)?.+?)\\s+?([_a-zA-Z].+?\\))");
    private static Pattern classPattern = Pattern.compile("(public\\s+?(?:class|interface|abstract\\s+?class)\\s+?(?:[_a-zA-Z].+?))\\s+?.*?\\{");

    private static String commentMatcher(String fileContent) {
        if (fileContent != null) {
            Matcher methodsMather = commentsMatcher.matcher(fileContent);

            if (methodsMather.find()) {
                return methodsMather.group(1);
            }
        }

        return "";
    }

    private static List<MethodInfo> matchMethods(String fileContent) {
        Matcher methodsMather = methodsPattern.matcher(fileContent);
        List<MethodInfo> methodInfos = new ArrayList<>();

        while (methodsMather.find()) {
            String method = methodsMather.group();
            String comment = commentMatcher(method);

            MethodInfo methodInfo = parseMethod(method);
            methodInfo.setMethodComments(comment);
            methodInfo.setMethodComments(comment);
            System.out.println(methodInfo);

            methodInfos.add(methodInfo);
        }

        return methodInfos;
    }


    private static MethodInfo parseMethod(String method)  {
        Matcher matcher = methodPattern.matcher(method);
        if (matcher.find()) {
            return new MethodInfo("", matcher.group(1), matcher.group(2));
        }
        throw new RuntimeException("匹配的方法有误");
    }


    public static ClassInfo getClassInfo(String filePath) {
        String content = loadFile(filePath);

        Matcher classMatcher = classPattern.matcher(content);

        String className = "";
        if (classMatcher.find()) {
            className = classMatcher.group(1);
        } else {
            throw new RuntimeException("该文件不是java文件!");
        }

        List<MethodInfo> methodInfos = matchMethods(content);
        return new ClassInfo("", className, methodInfos);
    }


    private static String loadFile(String filePath) {
        List<String> lines = null;
        try {
            lines = Files.readAllLines(Paths.get(filePath),
                    StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringBuilder sb = new StringBuilder();
        lines.forEach(s -> {
            sb.append(s + "\n\r");
        });

        return sb.toString();
    }

    String classComment;
    String className;
    List<MethodInfo> methodsInfo;


    public ClassInfo(String classComment, String className, List<MethodInfo> methodsInfo) {
        this.classComment = classComment;
        this.methodsInfo = methodsInfo;
        this.className = className;
    }

    public String getClassComment() {
        return classComment;
    }
    public List<MethodInfo> getMethodsInfo() {
        return methodsInfo;
    }

    public String getClassName() {
        return className;
    }


    @Override
    public String toString() {
        return "ClassInfo{" +
                "classComment='" + classComment + '\'' +
                ", className='" + className + '\'' +
                ", methodsInfo=" + methodsInfo +
                '}';
    }
}
